﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace KPIS.GERP.GBMS.BLL
{
    public class CommandControl
    {
        private readonly Util util = new Util();
        private List<Control> listControl = new List<Control>();
        public bool IsFormValid { get; set; }

        public CommandControl()
        {
            IsFormValid = true;
        }
        
        public void AddNewCommand(Control pnList, Control pnGrid, Control pnCommand)
        {
   
            ClearPanel(pnList);

            SetPanelList(pnList, "AddNew");
            SetPanelCommand(pnCommand, "AddNew");

            pnList.Visible = true;
            pnGrid.Visible = false;
        }

        public void EditCommand(Control pnList, Control pnGrid, Control pnCommand)
        {
            SetPanelList(pnList, "Edit");
            SetPanelCommand(pnCommand, "Edit");

            pnList.Visible = true;
            pnGrid.Visible = false;
        }

        public void CancelCommand(Control pnList, Control pnGrid, Control pnCommand)
        {
            SetPanelList(pnList, "Cancel");
            SetPanelCommand(pnCommand, "Cancel");
        }

        public void GridCommand(Control pnList, Control pnGrid, Control pnCommand)
        {
            ClearPanel(pnList);
            SetPanelCommand(pnCommand, "Grid");

            pnList.Visible = false;
            pnGrid.Visible = true;
        }

        public void ViewCommand(Control pnList, Control pnGrid, Control pnCommand)
        {
            if (IsFormValid)
            {
                var lc = GetAllControl(pnList);
                foreach (Control ctrl in lc)
                {
                    switch (ctrl.GetType().ToString())
                    {
                        case "System.Web.UI.WebControls.Label":
                            {
                                Label lb = (Label)ctrl;

                                foreach (Control mapControl in lc)
                                {
                                    if (mapControl.HasControls())
                                    {
                                        Control ucControl = mapControl.FindControl(lb.AssociatedControlID);

                                        if (mapChildControl(ucControl, lb))
                                            break;
                                    }

                                    if (mapChildControl(mapControl, lb))
                                        break;
                                }
                                break;
                            } 
                    }
                }

                SetPanelList(pnList, "View");
                SetPanelCommand(pnCommand, "View");

                pnList.Visible = true;
                pnGrid.Visible = false;
            }
        }

        private bool mapChildControl(Control mapControl, Label lb)
        {
            bool bFound = false;

            if (mapControl != null)
            {
                string typeName = mapControl.GetType().ToString();
                switch (typeName)
                {
                    case "System.Web.UI.WebControls.TextBox":
                        {
                            if (((TextBox)mapControl).ID == lb.AssociatedControlID)
                            {
                                lb.Text = ((TextBox)mapControl).Text;
                                bFound = true;
                            }
                            break;
                        }
                    case "Telerik.Web.UI.RadTextBox":
                        {
                            if (((RadTextBox)mapControl).ID == lb.AssociatedControlID)
                            {
                                lb.Text = ((RadTextBox)mapControl).Text;
                                bFound = true;
                            }
                            break;
                        }
                    case "Telerik.Web.UI.RadNumericTextBox":
                        {
                            if (((RadNumericTextBox)mapControl).ID == lb.AssociatedControlID)
                            {
                                var ntx = ((RadNumericTextBox) mapControl);
                                bFound = true;
                                try
                                {
                                    int decimalCount = ntx.NumberFormat.DecimalDigits;
                                    string groupSeperator = ntx.NumberFormat.GroupSeparator;
                                    string decimalSeperator = ntx.NumberFormat.DecimalSeparator;
                                    string decimalCountX0 = String.Empty.PadLeft(decimalCount, '0');
                                    string formatString = "{0:#" + groupSeperator + "##0" + decimalSeperator + decimalCountX0 + "}";
                                    lb.Text = String.Format(formatString, util.AlwayDouble(Convert.ToString(ntx.Value)));
                                }
                                catch
                                {
                                    lb.Text = ntx.Text;
                                }
                            }

                            break;
                        }
                    case "Telerik.Web.UI.RadComboBox":
                        {
                            if (((RadComboBox)mapControl).ID == lb.AssociatedControlID)
                            {
                                lb.Text = ((RadComboBox) mapControl).SelectedValue.Length > 0
                                              ? ((RadComboBox) mapControl).SelectedItem.Text
                                              : "";
                                bFound = true;
                            }

                            break;
                        }
                }
            }

            return bFound;
        }

        private void SetPanelList(Control pnList, string mode)
        {
            foreach (Control ctrl in pnList.Controls)
            {
                if (ctrl.HasControls())
                {
                    for (int i = 0; i < ctrl.Controls.Count; i++)
                    {
                        if (ctrl.Controls[i].GetType().ToString() == "System.Web.UI.WebControls.Panel")
                        {
                            SetPanelList(ctrl.Controls[i], mode);
                                                    }
                        else
                        {
                            SetVisibleControl(ctrl.Controls[i], mode);
                        }
                    }
                }

                SetVisibleControl(ctrl, mode);
            }
        }

        private void SetVisibleControl(Control ctrl, string mode)
        {
            switch (ctrl.GetType().ToString())
            {
                case "System.Web.UI.WebControls.Label":
                    ((Label)ctrl).Visible = (mode == "View") || (mode == "Cancel");
                    break;
                //case "System.Web.UI.WebControls.Literal":
                //    ((Literal)ctrl).Visible = (mode == "View") || (mode == "Cancel");
                //    break;
                case "System.Web.UI.WebControls.TextBox":
                    ((TextBox)ctrl).Visible = (mode == "AddNew" || mode == "Edit") &&  !(mode == "Cancel");
                    break;
                case "System.Web.UI.WebControls.DropDownList":
                    ((DropDownList)ctrl).Visible = (mode == "AddNew" || mode == "Edit") && !(mode == "Cancel");
                    break;
                case "System.Web.UI.WebControls.RadioButtonList":
                    ((RadioButtonList)ctrl).Visible = (mode == "AddNew" || mode == "Edit") && !(mode == "Cancel");
                    break;
                case "System.Web.UI.WebControls.CheckBox":
                    ((CheckBox)ctrl).Visible = (mode == "AddNew" || mode == "Edit") && !(mode == "Cancel");
                    break;

                // Telerix Control
                case "Telerik.Web.UI.RadComboBox":
                    ((RadComboBox)ctrl).Visible = (mode == "AddNew" || mode == "Edit") && !(mode == "Cancel");
                    break;
                case "Telerik.Web.UI.RadTextBox":
                    ((RadTextBox)ctrl).Visible = (mode == "AddNew" || mode == "Edit") && !(mode == "Cancel");
                    break;
                case "Telerik.Web.UI.RadNumericTextBox":
                    ((RadNumericTextBox)ctrl).Visible = (mode == "AddNew" || mode == "Edit") && !(mode == "Cancel");
                    break;
            }
        }

        public void SetPanelCommand(Control pnCommand, string mode)
        {
	        bool found = false;
			foreach (Control ctrl in pnCommand.Controls)
			{
				switch (ctrl.GetType().ToString())
				{
					case "System.Web.UI.WebControls.LinkButton":
						{
							found = true;
							if (((LinkButton) ctrl).ID == "AddNew")
							{
								((LinkButton) ctrl).Visible = (mode == "AddNew" || mode == "Grid");
							}
							else if (((LinkButton) ctrl).ID == "Edit")
							{
								((LinkButton) ctrl).Visible = !(mode == "AddNew" || mode == "Grid") || (mode == "Cancel");
							}
							else if (((LinkButton) ctrl).ID == "Delete")
							{
								((LinkButton) ctrl).Visible = (!(mode == "AddNew") && !(mode == "Edit")) || (mode == "Cancel");
							}
							else if (((LinkButton) ctrl).ID == "Save_AddNew")
							{
								((LinkButton) ctrl).Enabled = (mode == "AddNew");
								((LinkButton) ctrl).Visible = (mode == "AddNew");
							}
							else if (((LinkButton) ctrl).ID == "Save_Edit")
							{
								((LinkButton) ctrl).Enabled = (mode == "Edit");
								((LinkButton) ctrl).Visible = (mode == "Edit");
							}
							else if (((LinkButton) ctrl).ID == "Cancel")
							{
								((LinkButton) ctrl).Enabled = (mode == "Edit");
								((LinkButton) ctrl).Visible = (mode == "Edit");
							}

							break;
						}
				}
			}
			if(!found)
				FindControl2(pnCommand, mode);
        }

		private void FindControl2(Control pnCommand, string mode)
		{
			Control AddNew = FindControlRecursive(pnCommand, "AddNew");
			Control Edit = FindControlRecursive(pnCommand, "Edit");
			Control Delete = FindControlRecursive(pnCommand, "Delete");
			Control Save_AddNew = FindControlRecursive(pnCommand, "Save_AddNew");
			Control Save_Edit = FindControlRecursive(pnCommand, "Save_Edit");
			Control Cancel = FindControlRecursive(pnCommand, "Cancel");
			if (AddNew != null)
			{
				((LinkButton)AddNew).Visible = (mode == "AddNew" || mode == "Grid");
			}
			if (Edit != null)
			{
				((LinkButton)Edit).Visible = !(mode == "AddNew" || mode == "Grid") || (mode == "Cancel");
			}
			if (Delete != null)
			{
				((LinkButton)Delete).Visible = (!(mode == "AddNew") && !(mode == "Edit")) || (mode == "Cancel");
			}
			if (Save_AddNew != null)
			{
				((LinkButton)Save_AddNew).Enabled = (mode == "AddNew");
				((LinkButton)Save_AddNew).Visible = (mode == "AddNew");
			}
			if (Save_Edit != null)
			{
				((LinkButton)Save_Edit).Enabled = (mode == "Edit");
				((LinkButton)Save_Edit).Visible = (mode == "Edit");
			}
			if (Cancel != null)
			{
				((LinkButton)Cancel).Enabled = (mode == "Edit");
				((LinkButton)Cancel).Visible = (mode == "Edit");
			}
		}

		public Control FindControlRecursive(Control root, string id)
		{
			if (root.ID == id) return root;

			foreach (Control ctl in root.Controls)
			{
				Control foundCtl = FindControlRecursive(ctl, id);
				if (foundCtl != null) return foundCtl;
			}

			return null;
		}

        public List<Control> GetAllControl(Control root)
        {
            listControl.Clear();
            GetAllControlRecursive(root);
            return listControl;
        }

        private void GetAllControlRecursive(Control root)
        {
			foreach (Control ctl in root.Controls)
			{
                listControl.Add(ctl);
                GetAllControlRecursive(ctl);
			}
        }

        private void ClearPanel(Control pnList)
        {
            foreach (Control ctrl in pnList.Controls)
            {
                if (ctrl.HasControls())
                {
                    for (int i = 0; i < ctrl.Controls.Count; i++)
                    {
                        ResetData(ctrl.Controls[i]);
                    }
                }

                ResetData(ctrl);
            }
        }

        private void ResetData(Control ctrl)
        {
            switch (ctrl.GetType().ToString())
            {
                case "System.Web.UI.WebControls.Label":
                    ((Label)ctrl).Text = String.Empty;
                    break;
                case "System.Web.UI.WebControls.TextBox":
                    ((TextBox)ctrl).Text = String.Empty;
                    break;
                case "System.Web.UI.WebControls.DropDownList":
                    ((DropDownList)ctrl).SelectedIndex = 0;
                    break;
                case "System.Web.UI.WebControls.CheckBox":
                    ((CheckBox)ctrl).Checked = false;
                    break;

                // Telerix Control
                case "Telerik.Web.UI.RadComboBox":
                    {
                        var cbb = ((RadComboBox) ctrl);
                        if (cbb.ID == "ysCombo" || cbb.ID == "yeCombo")
                        {
                            try
                            {
                                cbb.SelectedIndex =
                                    cbb.Items.FindItemByValue(
                                        DateTime.Now.ToString("yyy",
                                          CultureInfo.GetCultureInfo("th-TH"))).
                                            Index;
                            } catch
                            {
                            }
                        }
                        else if (cbb.Items.Count > 0)
                            cbb.SelectedIndex = 0;
                        break;
                    }
                case "Telerik.Web.UI.RadNumericTextBox":
                    ((RadNumericTextBox) ctrl).Value = null;
                    break;
                case "Telerik.Web.UI.RadTextBox":
                    ((RadTextBox)ctrl).Text = String.Empty;
                    break;
            }
        }
    }
}
